package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Thoughtworker
 * Date: 10/15/13
 * Time: 5:07 PM
 * To change this template use File | Settings | File Templates.
 */
public class LoginHandler {


    private final PrintStream printStream;
    private final BufferedReader reader;
    private Map<String, LibraryMember> users;
    public String loggedInUser = "";

    public LoginHandler(PrintStream printStream, BufferedReader reader) {
        /* To change body of created methods use File | Settings | File Templates. */
        this.printStream = printStream;
        this.reader = reader;
        users = new HashMap<String, LibraryMember>();
        users.put("1111111", new LibraryMember("1", "Susana", "105 Street, Chicago, IL, 60601", "405-111-2222"));


    }

    public boolean verifyUser() throws IOException {



        printStream.println("Please Type Your UserID:");
        String userID= reader.readLine();
        printStream.println("Please Type Your Password:");
        String password = reader.readLine();


        return isUserLoginValid(userID, password);

    }

    public boolean isUserLoginValid(String userID, String password) {
        boolean isValidUser = false;
        if(users.containsKey(userID)){
            if(users.get(userID).verifyPassword(password)){
                loggedInUser = userID;
                printStream.println("You are logged in!!");
            }else{
                printStream.println("Incorrect Username and Password\nPlease talk to Librarian");
            }
        }else{
            printStream.println("Incorrect Username and Password\nPlease talk to Librarian");

        }
        return isValidUser;
    }

    public void checkMembership() {

        printStream.println(users.get(loggedInUser).getInfo());
    }
}
